package cn.mickey.exchange.api;

import cn.mickey.exchange.annotation.LoginUser;
import cn.mickey.exchange.annotation.RedisCache;
import cn.mickey.exchange.domain.JsonResponse;
import cn.mickey.exchange.domain.model.IdleItemModel;
import cn.mickey.exchange.domain.model.UserModel;
import cn.mickey.exchange.service.IdleItemService;
import cn.mickey.exchange.util.PageUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RedisCache
@RestController
@RequestMapping("/idle")
public class IdleItemApi {

    @Resource
    private IdleItemService idleItemService;

    @PostMapping("/add")
    public JsonResponse addIdleItem(@LoginUser UserModel user,
                                    @RequestBody IdleItemModel idleItemModel) {
        idleItemModel.setUserId(user.getUserId());
        idleItemModel.setIdleStatus((byte) 1);
        if (idleItemService.addIdleItem(idleItemModel)) {
            return JsonResponse.success(idleItemModel);
        }
        return JsonResponse.fail("请重新上传!");
    }

    @GetMapping("/find")
    public JsonResponse findIdleItem(@RequestParam(value = "findValue", required = false) String findValue,
                                     @RequestParam(value = "page", required = false) Integer page,
                                     @RequestParam(value = "nums", required = false) Integer nums) {
        if (null == findValue) {
            findValue = "";
        }
        Page<IdleItemModel> idleItem = idleItemService
                .findIdleItem(findValue, PageUtil.getPage(page), PageUtil.getNum(nums));
        return JsonResponse.success(idleItem);
    }

    @GetMapping("/label")
    public JsonResponse findIdleItemByLabel(@RequestParam(value = "idleLabel", required = true) Integer idleLabel,
                                            @RequestParam(value = "page", required = false) Integer page,
                                            @RequestParam(value = "nums", required = false) Integer nums) {
        Page<IdleItemModel> idleItemByLabel = idleItemService
                .findIdleItemByLabel(idleLabel, PageUtil.getPage(page), PageUtil.getNum(nums));
        return JsonResponse.success(idleItemByLabel);
    }

    @GetMapping("/info")
    public JsonResponse findIdleItem(@RequestParam Long id) {
        return JsonResponse.success(idleItemService.findIdleItem(id));
    }

    @GetMapping("/all")
    public JsonResponse getAllIdleItem(@LoginUser UserModel user) {
        return JsonResponse.success(idleItemService.getAllIdleItem(user.getUserId()));
    }


    @PostMapping("/update")
    public JsonResponse updateIdleItem(@LoginUser UserModel user,
                                       @RequestBody IdleItemModel idleItemModel) {
        idleItemModel.setUserId(user.getUserId());
        if (idleItemService.updateIdleItem(idleItemModel)) {
            return JsonResponse.success();
        }
        return JsonResponse.fail();
    }
}
